package com.itheima.behaviour.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.behaviour.dto.FollowBehaviourDto;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.behaviour.listener
 */
@Component
@Slf4j
public class FollowBehaviourListener {

    @Resource
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = BusinessConstants.MqConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void consumeMsg(ConsumerRecord<String,String> msg){
        int p = msg.partition();
        long o = msg.offset();
        String json = msg.value();
        log.info("关注行为记录开始: {},{},{}", p,o,json);
        // 解析消息转成java对象
        FollowBehaviourDto dto = JSON.parseObject(json, FollowBehaviourDto.class);
        apFollowBehaviorService.saveFollowBehavior(dto);
        log.info("关注行为记录结束: {},{},{}", p,o,json);
    }
}
